# coding:utf-8

import urllib2
from bs4 import BeautifulSoup

class UseRequest:
    def setHeader(self):
        url = 'http://www.baidu.com'
        header = {
            # 详细设置可以通过 Fiddler 4来参考进行设置
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
            'Connection': 'keep - alive',
            'Cache - Control': 'max - age = 0',
            'Upgrade-Insecure-Requests': '1',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            # 这句表明浏览器支持接收压缩编码的HTMl响应
            # 'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9'
        }
        return url,header

    def getRequest(self,url,header):
        return urllib2.Request(url,headers = header)

    def open(self,request):
        response = urllib2.urlopen(request)
        return response.read()

    def printResult(self,html):
        soup = BeautifulSoup(html,'html.parser')
        print(soup.get_text())

if __name__ == '__main__':
    u = UseRequest()
    url,header = u.setHeader()
    request = u.getRequest(url,header)
    html = u.open(request)
    # print(html)
    u.printResult(html)